﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using Microsoft.Win32;
using System.Diagnostics;

namespace BabyOffice
{
    public partial class Default : System.Web.UI.Page
    {
        private string FilePath
        {
            get
            {
                return Server.MapPath("~/") + "FileUploadPath\\"; 
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindFileGrid();
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            UploadFile();
            BindFileGrid();
        }

        private void UploadFile()
        {
            string filePath = this.FilePath;

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            if (fileUpload.HasFile)
            {
                string fileExtension = Path.GetExtension(fileUpload.FileName).ToLower();
                if (fileExtension != ".xls" && fileExtension != ".xlsx")
                {
                    return;
                }
            }

            string fileFullName = filePath + fileUpload.FileName;
            fileUpload.PostedFile.SaveAs(fileFullName);
        }

        private void BindFileGrid()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("CreationTime");
            dataTable.Columns.Add("FileName");
            dataTable.Columns.Add("FileFullName");

            DirectoryInfo directoryInfo = new DirectoryInfo(this.FilePath);
            if (!directoryInfo.Exists)
            {
                directoryInfo.Create();
            }

            FileInfo[] fileList = directoryInfo.GetFiles();

            foreach (FileInfo fileInfo in fileList)
            {
                dataTable.Rows.Add(fileInfo.LastWriteTime, fileInfo.Name, fileInfo.FullName);
            }

            dataTable.DefaultView.Sort = "CreationTime DESC";

            gvFiles.DataSource = dataTable;
            gvFiles.DataBind();
        }

        private void InitSheetList(string fileFullName)
        {
            cblSheets.Items.Clear();

            ExcelFile excelFile = new ExcelFile();
            excelFile.Open(fileFullName);
            List<string> sheetNameList = excelFile.GetSheetNameList();
            excelFile.Close();

            foreach (string sheetName in sheetNameList)
            {
                cblSheets.Items.Add(sheetName);
            }
        }

        public void SaveToRAR(string patch, string rarPatch, string rarName)
        {
            String the_rar;
            RegistryKey the_Reg;
            Object the_Obj;
            String the_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try
            {
                the_Reg = Registry.ClassesRoot.OpenSubKey(@"ApplicationsWinRAR.exeShellOpenCommand");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                the_rar = the_rar.Substring(1, the_rar.Length - 7);
                Directory.CreateDirectory(patch);
                //命令参数
                the_Info = " a    " + rarName + "  " + patch + "  -r"; ;
                the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //打包文件存放目录
                the_StartInfo.WorkingDirectory = rarPatch;
                the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void DeleteFile(string fileFullName)
        { 
        
        }

        protected void gvFiles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow gridViewRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
            string fileFullName = gvFiles.DataKeys[gridViewRow.RowIndex]["FileFullName"].ToString();

            switch (e.CommandName)
            {
                case "selectFile":
                    InitSheetList(fileFullName);
                    break;
                case "deleteFile":
                    File.Delete(fileFullName);
                    BindFileGrid();
                    break;
                default:
                    break;
            }            
        }
    }
}